function factor(a) {
    if(a==1) return 1;
    return a*factor(a-1);
}

function p24(limit) {
    var tmp1, tmp2, tmp3;
    var s=[], c=[];
    for(var i=0; i<10; i++) s[i] = i;
    tmp1 = limit;
    for(var i=9; i>=1; i--) {
        var n1= factor(i);
        tmp1 = Math.floor((tmp1-1)/n1);
        print(tmp1);
        c.push(s[tmp1])
        s.splice(tmp1, 1);
        tmp1 = limit % n1;
        if(tmp1 == 0) tmp1 = n1;
    }
    c.push(s[0]);
    return c.join("");
}

print(p24(1000001));
